﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using MakerHelper = ERP_RORZE_ORDER.BizLogic.MakerHelper;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.Forms
{
    public partial class frm_OrderDeliveryEdit : DevExpress.XtraEditors.XtraForm
    {
        private ucOrderDelivery parent;
        private DataTable dtProduct;

        public string OrderID;
        public string ProductID;
        public DateTime Date;
        public DateTime Deadline;
        public int Quantity;
        private LogUtils log;
        public string Status;

        public frm_OrderDeliveryEdit(ucOrderDelivery ucP)
        {
            InitializeComponent();
            parent = ucP;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
            this.ActiveControl = dateDate;
        }

        private void frm_ProductAdd_Load(object sender, EventArgs e)
        {
            BindData();
        }


        private void btEdit_Click(object sender, EventArgs e)
        {
            OrderDeliveryHelper helper = new OrderDeliveryHelper(new OrderDelivery());
            if (GetData(helper.ValueObject))
            {

                if (helper.Update() > 0)
                {
                    //write Log
                    log.Table = LogConstant.Table.F2Order.OrderDelivery;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "OrderID " + helper.ValueObject.TempId + "; Date: " + helper.ValueObject.Date;
                    log.WriteLog();


                    if (Utilities.MessageConfirmationYesNo(OrderConstants.Message.UpdateSuccessAndQuestion))
                    {

                        dateDate.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(OrderConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region Private Method
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private bool GetData(OrderDelivery o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dateDate.EditValue))
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.DateCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbOrderID.SelectedValue) || cmbOrderID.SelectedItem == cmbOrderID.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.OrderIDCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbProduct.SelectedValue) || cmbProduct.SelectedItem == cmbProduct.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.ProductCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(dateDeadline.EditValue))
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.DeadlineCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtQuantity.Text))
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.QuantityCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (int.Parse(txtQuantity.Text) <= 0)
            {
                MessageBox.Show(OrderConstants.Message.OrderDelivery.QuantityMustGreaterZero, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.TempId = cmbOrderID.SelectedValue.ToString();
            o.Date = dateDate.DateTime.Date;
            o.OrderID = OrderID;
            o.ProductID = cmbProduct.SelectedValue.ToString();
            o.Deadline = dateDeadline.DateTime.Date;
            o.Quantity = int.Parse(txtQuantity.Text);
            o.Status = txtStatus.Text;

            return true;
        }

        private void BindData()
        {
            //cmbOrderID.SelectedItem = OrderID;
            //cmbProduct.SelectedItem = ProductID;
            dateDate.EditValue = Date;
            dateDeadline.EditValue = Deadline;
            txtQuantity.Text = Quantity.ToString();
            txtStatus.Text = Status;
            BindDataOrder(); 
            BindDataProduct();
        }

        private void BindDataOrder()
        {
            OrderHelper helper = new OrderHelper(new Order());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            //row[OrderConstants.Entities.Order.FieldName.OrderID] = string.Empty;
            row[OrderConstants.Entities.Order.FieldName.OrderID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbOrderID.DataSource = dt;
            cmbOrderID.DisplayMember = OrderConstants.Entities.OrderDelivery.FieldName.OrderID;
            cmbOrderID.ValueMember = OrderConstants.Entities.OrderDelivery.FieldName.OrderID;

            cmbOrderID.SelectedValue = OrderID;
            if (cmbOrderID.SelectedItem == null)
            {
                cmbOrderID.SelectedItem = cmbOrderID.Items[0];
            }

        }

        private void BindDataProduct()
        {
            ProductHelper helper = new ProductHelper(new Product());
            dtProduct = helper.GetAll();
            DataRow row = dtProduct.NewRow();

            //row[OrderConstants.Entities.Product.FieldName.ProductID] = string.Empty;
            row[OrderConstants.Entities.Product.FieldName.ProductID] = null;
            dtProduct.Rows.InsertAt(row, 0);

            cmbProduct.DataSource = dtProduct;
            cmbProduct.DisplayMember = OrderConstants.Entities.Product.FieldName.ProductID;
            cmbProduct.ValueMember = OrderConstants.Entities.Product.FieldName.ProductID;

            cmbProduct.SelectedValue = ProductID;
            if (cmbProduct.SelectedItem == null)
            {
                cmbProduct.SelectedItem = cmbProduct.Items[0];
            }
        }

        #endregion

        private void cmbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!Utilities.IsNullOrEmpty(cmbProduct.SelectedValue) || cmbProduct.SelectedItem != cmbProduct.Items[0])
            {
                DataRow[] row = dtProduct.Select(OrderConstants.Entities.Product.FieldName.ProductID + " = '" + cmbProduct.SelectedValue.ToString() + "'");
                if (row.Length > 0)
                {
                    txtUnit.Text = row[0][OrderConstants.Entities.Product.FieldName.UnitID].ToString();
                    txtPrice.Text = (decimal.Parse(row[0][OrderConstants.Entities.Product.FieldName.Price].ToString()).ToString("n2"));
                    txtCurrencyPrice.Text = row[0][OrderConstants.Entities.Product.FieldName.CurrencyID].ToString();
                    txtProductName.Text = row[0][OrderConstants.Entities.Product.FieldName.ProductName].ToString();
                    txtEnglishName.Text = row[0][OrderConstants.Entities.Product.FieldName.EnglishName].ToString();
                    txtxVietName.Text = row[0][OrderConstants.Entities.Product.FieldName.VietName].ToString();
                    txtJapanName.Text = row[0][OrderConstants.Entities.Product.FieldName.JapanName].ToString();
                    txtSize.Text = row[0][OrderConstants.Entities.Product.FieldName.Size].ToString();
                    txtMakername.Text = row[0][OrderConstants.Entities.Product.FieldName.MakerName].ToString();
                    txtProducrGroup.Text = row[0][OrderConstants.Entities.Product.FieldName.GroupName].ToString();
                    txtSupplierName.Text = row[0][OrderConstants.Entities.Product.FieldName.SupplierName].ToString();
                    txtPurpose.Text = row[0][OrderConstants.Entities.Product.FieldName.PurposeName].ToString();
                    txtKind.Text = row[0][OrderConstants.Entities.Product.FieldName.KindID].ToString();
                }
            }
        }

        private void frm_OrderDeliveryEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                cmbOrderID.Focus();
            }
        }


    }
}